package weka.filters.timeseries.shapelet_transforms.sample;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import development.DataSets;
import weka.core.Instance;
import weka.core.Instances;

public class MinDistanceSample {
	public List<Integer> getSampleIndex(Instances instances){
		double[] dist=new double[instances.numInstances()];
		for(int i=0;i<instances.numInstances();i++){
			dist[i]=0;
		}
		for(int i=0;i<instances.numInstances()-1;i++){
			for(int j=i+1;j<instances.numInstances();j++){
				if(instances.get(i).classValue()==instances.get(j).classValue()){
					Instance iInstance=instances.get(i);
					Instance jInstance=instances.get(j);
					double distance=0;
					for(int k=0;k<instances.numAttributes()-1;k++)
						distance+=Math.pow(iInstance.value(k)-jInstance.value(k), 2);
					distance=Math.sqrt(distance);		
					dist[i]+=distance;
					dist[j]+=distance;
				}
			}
		}
		Map<Double,Integer> sampleIndex=new HashMap<Double,Integer>();
		for(int i=0;i<instances.numInstances();i++){
			if(sampleIndex.containsKey(instances.get(i).classValue())){
				int j=sampleIndex.get(instances.get(i).classValue());
				if(dist[i]<dist[j]){
					sampleIndex.put(instances.get(i).classValue(),i);
				}
				
			}else{
				sampleIndex.put(instances.get(i).classValue(),i);
			}
		}
		
		Iterator entries = sampleIndex.entrySet().iterator();  
		List<Integer> list=new ArrayList<Integer>(); 
		while (entries.hasNext()) {  		  
		    Map.Entry entry = (Map.Entry) entries.next();  
		    Integer value = (Integer)entry.getValue();  
		    list.add(value); 	  
		} 
		return list;
	}
	
	public static void main(String[] args){
		final String resampleLocation = DataSets.problemPath;
		final String dataset = "ECG200";
		final String filePath = resampleLocation + File.separator + dataset + File.separator + dataset;
		Instances test, train;
		test = utilities.ClassifierTools.loadData(filePath + "_TEST");
		train = utilities.ClassifierTools.loadData(filePath + "_TRAIN");
		List<Integer> list=new MinDistanceSample().getSampleIndex(test);
		for(int i=0;i<list.size();i++){
			System.out.println(list.get(i));
		}
	}
}
